package org.klarton.filter;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * This is an abstract class for a filter. It allows filters to hold their name,
 * target frequency and their current value.
 */
public abstract class Filter {

    public static final String VALUE = "value";
    public static final String TARGET_FREQUENCY = "targetFrequency";
    private final PropertyChangeSupport support = new PropertyChangeSupport(this);
    private double value = 0;
    private double targetFrequency;
    private String name;

    protected Filter(String name, double targetFrequency) {
        this.name = name;
        this.targetFrequency = targetFrequency;
    }

    /**
     * Give the filter the next sample to read.
     * @param samples to read
     */
    public abstract void read(double... samples);

    public void setValue(double value) {
        double old = this.value;
        this.value = value;
        support.firePropertyChange(VALUE, old, value);
    }

    /**
     * @return the current filter value. 
     */
    public double getValue() {
        return value;
    }

    public void setTargetFrequency(double targetFrequency) {
        double old = this.targetFrequency;
        this.targetFrequency = targetFrequency;
        support.firePropertyChange(TARGET_FREQUENCY, old, targetFrequency);
    }

    public double getTargetFrequency() {
        return targetFrequency;
    }

    public String getName() {
        return name;
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        support.addPropertyChangeListener(listener);
    }

    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        support.addPropertyChangeListener(propertyName, listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        support.removePropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        support.removePropertyChangeListener(propertyName, listener);
    }
}
